package com.ggs.rg.ybjb.systemmodule.shiro;


import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * @Description 自定义获取Token
 * @Author Sans
 * @CreateTime 2019/6/13 8:34
 */
public class ShiroSessionManager extends DefaultWebSessionManager {
    //定义常量
    private static final String AUTHORIZATION = "Authorization";
    private static final String REFERENCED_SESSION_ID_SOURCE = "Stateless request";

    //重写构造器
    public ShiroSessionManager() {
        super();
        this.setDeleteInvalidSessions(true);
    }

    /**
     * 重写方法实现从请求头获取Token便于接口统一
     * 每次请求进来,Shiro会去从请求头找Authorization这个key对应的Value(Token)
     *
     * @Author Sans
     * @CreateTime 2019/6/13 8:47
     */
    @Override
    public Serializable getSessionId(ServletRequest request, ServletResponse response) {
        //获取请求头Authorization中的数据
        String token = WebUtils.toHttp(request).getHeader(AUTHORIZATION);
        //如果请求头中存在token 则从请求头中获取token
        if (!StringTools.isNullOrEmpty(token)) {
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, token);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
            return token;
        } else {
            //如果没有携带，生成新的sessionId
            System.out.println("携带token,直接使用");
            return super.getSessionId(request, response);
        }
    }
}
//

// 这里禁用掉Cookie获取方式
// 按默认规则从Cookie取Token
// return super.getSessionId(request, response);
//            return null;








//    //获取请求头Authorization中的数据
//    String id = null;
//    //        String id = WebUtils.toHttp(request).getHeader("JSESSIONID");
//    Cookie[] cookies = WebUtils.toHttp(request).getCookies();
//        if (cookies != null) {
//                for (Cookie cookie : cookies) {
//                if ("shiro.session.id".equals(cookie.getName()))
//                id = cookie.getValue();
//                }
//                }
//                if (StringTools.isNullOrEmpty(id)) {
//                //如果没有携带，生成新的sessionId
//                System.out.println("如果没有携带，生成新的sessionId");
//                return super.getSessionId(request, response);
//                } else {
////            返回sessionId；
////            System.out.println("有携带sessionId");
////            System.out.println("sessionId的值为:" + id);
//                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "header");
//                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
//                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
//                return id;
//                }